package com.ruoyi.qymlxin.esspringboot;

import com.ruoyi.qymlxin.domain.IShopmsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.List;

/**
 * @author geng
 * 2020/12/19
 */
@Slf4j
@Service
public class BookService {

    private final ESBookRepository esBookRepository;


    public BookService(
                       ESBookRepository esBookRepository) {

        this.esBookRepository = esBookRepository;

    }

    public void addBook(IShopmsg saveESBook) {

        final ESBook esBook = new ESBook();
        assert saveESBook != null;
        BeanUtils.copyProperties(saveESBook, esBook);
        esBook.setId(saveESBook.getId() + "");
        try {
            esBookRepository.save(esBook);
        } catch (Exception e) {
            log.error(String.format("保存ES错误！%s", e.getMessage()));
        }
    }

    public void deleteBook(IShopmsg saveESBook) {
        final ESBook esBook = new ESBook();
        assert saveESBook != null;
        BeanUtils.copyProperties(saveESBook, esBook);
        esBook.setId(saveESBook.getId() + "");
        try {
            if(esBookRepository.existsById(esBook.getId())){
                esBookRepository.delete(esBook);
            }else {
                log.error("删除ES失败，不存在！%s");
            }
        } catch (Exception e) {
            log.error(String.format("删除ES错误！%s", e.getMessage()));
        }
    }

}
